using System;
using System.Collections.Generic;

namespace CLELCore.Assembly.CLELInstructions
{
	public class Switch : CLELInstruction
	{
		private List<Int32> _params;
		public List<Int32> Params
		{
			get
			{
				return _params;
			}
			set
			{
				_params = value;
			}
		}
		public override int Length
		{
			get
			{
				return 5 + (_params.Count*4);
			}
		}
		public Switch(List<Int32> param) : base(CLELOpcode.SWITCH)
		{
			_params = param;
		}

		public override byte[] GetBytes(bool little_endian)
		{
			byte[] ret = new byte[5+(_params.Count*4)];
			ret[0] = CLELOpcode.SWITCH;
			int i,num_parms = _params.Count;
			int parm,j;
			//write count
			if(little_endian)
			{
				for(i = 0;i <= 3;i++)
					ret[i+1] = Bits.ByteFromInt(i,num_parms);
			}
			else
			{
				for(i = 0;i <= 3;i++)
					ret[4-i] = Bits.ByteFromInt(i,num_parms);
			}
			//write parms
			for(i = 0;i < num_parms;i++)
			{
				parm = _params[i];
				if(little_endian)
				{
					for(j = 0;j <= 3;j++)
					{
						ret[(i*4)+j+5] = Bits.ByteFromInt(j,parm);
					}
				}
				else
				{
					for(j = 0;j <= 3;j++)
					{
						ret[(i*4)+8-j] = Bits.ByteFromInt(j,parm);
					}
				}
			}
			return ret;
		}
		
		public override string ToString()
		{
			string ret = "switch ";
			int i, num_params = _params.Count;
			for(i = 0;i < num_params;i++)
			{
				ret += _params[i];
				if(i < num_params-1)
					ret += " ";
			}
			return ret;
		}
	}
}
